package top.yuyufeng.mbuy.web;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import top.yuyufeng.mbuy.dto.CartDTO;
import top.yuyufeng.mbuy.dto.ResponseJson;
import top.yuyufeng.mbuy.enums.CartCheckedEnum;
import top.yuyufeng.mbuy.query.GetPostageQuery;
import top.yuyufeng.mbuy.service.ICartService;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author yyf19
 */
@Controller
@RequestMapping(value = "user/{userId}/cart")
public class CartController {
    @Autowired
    private ICartService cartService;


    @RequestMapping(value = "toCart", method = RequestMethod.GET)
    public String toPage() {
        return "cart";
    }

    @RequestMapping(value = "toSubmit", method = RequestMethod.GET)
    public String toSubmit() {
        return "submit";
    }


    @RequestMapping(value = "add", method = RequestMethod.POST)
    public @ResponseBody
    ResponseJson add(@RequestBody CartDTO cartDTO, @PathVariable("userId") Integer userId) {
        cartDTO.setUserId(userId);
        cartDTO.setChecked(CartCheckedEnum.UNSELECTED.getKey());
        try {
            cartService.add(cartDTO);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseJson.fail("加入购物车失败");
        }
        return ResponseJson.success("成功");
    }

    @RequestMapping(value = "list", method = RequestMethod.GET)
    public @ResponseBody
    ResponseJson<List<CartDTO>> list(@PathVariable("userId") Integer userId) {
        List<CartDTO> cartDTOList = cartService.listByUserId(userId);
        return ResponseJson.success(cartDTOList);
    }

    @RequestMapping(value = "listDetail", method = RequestMethod.GET)
    public @ResponseBody
    ResponseJson<List<CartDTO>> listDetail(@PathVariable("userId") Integer userId) {
        List<CartDTO> cartDTOList = cartService.listByUserIdWithDetail(userId);
        return ResponseJson.success(cartDTOList);
    }

    @RequestMapping(value = "listDetailChecked", method = RequestMethod.GET)
    public @ResponseBody
    ResponseJson<List<CartDTO>> listDetailChecked(@PathVariable("userId") Integer userId) {
        List<CartDTO> cartDTOList = cartService.listByUserIdWithDetailChecked(userId);
        return ResponseJson.success(cartDTOList);
    }

    @RequestMapping(value = "delete")
    public @ResponseBody
    ResponseJson delete(@PathVariable("userId") Integer userId, @RequestBody CartDTO cartDTO) {
        cartService.deleteById(cartDTO.getId());
        return ResponseJson.success();
    }


    @RequestMapping(value = "deleteAll", method = RequestMethod.GET)
    public @ResponseBody
    ResponseJson deleteAll(@PathVariable("userId") Integer userId) {
        cartService.deleteByUserId(userId);
        return ResponseJson.success("成功");
    }

    @RequestMapping(value = "check", method = RequestMethod.POST)
    public @ResponseBody
    ResponseJson check(@PathVariable("userId") Integer userId, @RequestBody CartDTO cartDTO) {
        cartService.check(userId,cartDTO.getId(),cartDTO.getChecked());
        return ResponseJson.success();
    }


}
